﻿using System;
using System.Collections.Generic;
using System.Windows;
using CarsRegistrationMobile.Managers;
using Microsoft.Phone.Controls;

namespace CarsRegistrationMobile.Views
{
    public partial class Menu : PhoneApplicationPage
    {
        private string newRequestPath;

        public Menu()
        {
            InitializeComponent();

            this.newRequestPath = "/views/Request.xaml";
        }

        private void ButtonNewRequest_Click(object sender, RoutedEventArgs e)
        {
            if(string.IsNullOrWhiteSpace(this.TextBoxName.Text) ||
               string.IsNullOrWhiteSpace(this.TextBoxNumber.Text))
            {
                MessageBox.Show("Введите имя владельца и номер транспортного средства");
                return;
            }
            GotRequestResult callback =
                (jsonResult) =>
                {
                    if (jsonResult != null)
                    {
                        ((App)Application.Current).RequestID = jsonResult.RequestID;

                        Dispatcher.BeginInvoke(
                            () =>
                            {
                                Uri uri = new Uri(this.newRequestPath, UriKind.Relative);
                                NavigationService.Navigate(uri);
                            });
                    }
                    else
                    {
                        Dispatcher.BeginInvoke(() =>
                        {
                            MessageBox.Show("ERROR");
                        });
                    }
                };

            var networkManager = ((App)Application.Current).NetworkManager;
            networkManager.BeginSendRequest(this.TextBoxName.Text, this.TextBoxNumber.Text, callback);
        }
    }
}